package br.ita.doacoes.view.controledoacoes;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.swingBean.actions.ApplicationAction;
import org.swingBean.descriptor.BeanTableModel;
import org.swingBean.descriptor.GenericFieldDescriptor;
import org.swingBean.descriptor.TableFieldDescriptor;
import org.swingBean.descriptor.XMLDescriptorFactory;
import org.swingBean.gui.JActButton;
import org.swingBean.gui.JBeanPanel;	
import org.swingBean.gui.JBeanTable;

import br.ita.doacoes.core.cadastrofamilia.dao.DAO;
import br.ita.doacoes.domain.cadastrodoacoes.Item;
import br.ita.doacoes.view.cadastrodoacoes.refresher.Refresher;

/**
 * @author Lucas Amaro, Fleuri e Sheyne
 * @since 10/12/2007
 * 
 * Classe que retorna o estoque ;)
 * 
 **/

public class ConsultaEstoqueTemplatePanel extends JPanel {
			
	protected JBeanPanel<Item> panelFormulario;
	protected JPanel panelBotao;	
	protected JActButton botaoFiltrar;
	protected JActButton botaoListarTodos;
	protected JActButton botaoLimpar;
	protected Object currentOnForm;	
	protected BeanTableModel tableModel;
	protected JBeanTable table;
	
	private String xmlForm;
	private String xmlTable;
	private String formName;
	private String tableName;
	private Class classe;
	private DAO dao;
						
	public void init() {

		TableFieldDescriptor tableDescriptor = XMLDescriptorFactory.getTableFieldDescriptor(classe, xmlTable, tableName);		
		tableModel = new BeanTableModel<Item>(tableDescriptor);
		table = new JBeanTable(tableModel);
		
		tableModel.orderByProperty("nome");
		tableModel.setBeanList(dao.getList());
		table.enableQuickEditing();
		
		botaoFiltrar = new JActButton("Filtrar",
				new ApplicationAction() {
					public void execute() {
						
						Item item = new Item();
						panelFormulario.populateBean(item);
						tableModel.cleanFilter();
						//tableModel.setContinuousFiltering(true);
						if (item.getTipo() != null)
							tableModel.filterContains("tipo.nome", item.getTipo().getNome());
						//if (item.getLocal() != null)
							//tableModel.filterContains("local.nome", item.getLocal().getNome());
						//if (item.getValidade() != null)
							//tableModel.filterBetween("validade", null, item.getValidade().toString());
						//tableModel.setContinuousFiltering(false);
						
					}
				});
		
		botaoListarTodos = new JActButton("Listar Todos",
				new ApplicationAction() {
					public void execute() {
						tableModel.setBeanList(dao.getList());
					}
				});
		
		botaoLimpar = new JActButton("Limpar Formulario",
				new ApplicationAction() {
					public void execute() {
						panelFormulario.cleanForm();
					}
				});

		panelBotao = new JPanel();
		panelBotao.setLayout(new GridLayout());
		panelBotao.add(botaoFiltrar);
		panelBotao.add(botaoListarTodos);
		panelBotao.add(botaoLimpar);

		GenericFieldDescriptor formdescriptor = XMLDescriptorFactory.getFieldDescriptor(classe, xmlForm, formName);
		panelFormulario = new JBeanPanel<Item>(classe, formdescriptor);
		
		setLayout(new BorderLayout());
		add(panelFormulario,BorderLayout.NORTH);
		add(new JScrollPane(table), BorderLayout.CENTER);
		add(panelBotao,BorderLayout.SOUTH);
	}
		
	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}

	public Class getClasse() {
		return classe;
	}

	public void setClasse(Class classe) {
		this.classe = classe;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getXmlForm() {
		return xmlForm;
	}

	public void setXmlForm(String xmlForm) {
		this.xmlForm = xmlForm;
	}

	public String getXmlTable() {
		return xmlTable;
	}

	public void setXmlTable(String xmlTable) {
		this.xmlTable = xmlTable;
	}	

}

